//
//  PlayVideoViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 JohnnyB. All rights reserved.
//

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS

#import "PlayVideoViewController.h"
#import "Header.h"
#import "ModelOfData.h"
#import "ModelOfCover.h"
#import "PlayingVideoViewController.h"
#import "CoolVideoViewController.h"
@interface PlayVideoViewController ()

@property (nonatomic, retain) UIImageView *playImageview;
@property (nonatomic, retain) UIImageView *burredImageview;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *detilLabel;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UIImageView *whiteLine;
@end

@implementation PlayVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self creatView];
    [self tapButton];
}




#pragma mark - hiddenTabbar

-(void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = YES;
    [UIView animateWithDuration:0.01f animations:^{
        
    
    if ([UIDevice currentDevice].orientation != UIDeviceOrientationPortrait){
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    }];
    [super viewWillAppear:animated];
}

-(void)creatView{
    
    self.playImageview = [UIImageView new];
    [self.view addSubview:self.playImageview];
    self.playImageview.layer.masksToBounds = YES;//图片围绕显示的 图片 渲染  卡顿感 的原因是 图片太大，卡顿的是没有在视图出现的图片
    
    [self.playImageview makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(SCREEN_HEIGHT * 4 / 7);
        
    }];
    
    self.burredImageview = [UIImageView new];
    [self.view addSubview:self.burredImageview];
    
    [self.burredImageview makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.playImageview.bottom).with.offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
        
        
    }];
    
    //标题
    self.titleLabel = [UILabel new];
    [self.burredImageview addSubview:self.titleLabel];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(10);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(30);
        
    }];
    
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:20 weight:3];
    
    //白条
    CGFloat height = 1;
    CGRect newRect = [self.modelOfDate.title boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20 ]} context:nil];
    
    
    self.whiteLine = [[UIImageView alloc] initWithFrame:CGRectMake(20, 45, newRect.size.width - 30, 1)];
    self.whiteLine.backgroundColor = [UIColor whiteColor];
    [self.burredImageview addSubview:self.whiteLine];
    
    
    //时间
    NSInteger time = [self.modelOfDate.duration  integerValue];
    NSString *timeString = [NSString stringWithFormat:@"%02ld'%2ld''", time/60, time%60];
    NSString *string = [NSString stringWithFormat:@"#%@ / %@", self.modelOfDate.cate, timeString];
    
    self.timeLabel = [UILabel new];
    
    [self.burredImageview addSubview:self.timeLabel];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.whiteLine.bottom).with.offset(10);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(20);
        
        
    }];
    
    self.timeLabel.text = string;
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    //    self.timeLabel.backgroundColor = [UIColor redColor];
    //    self.timeLabel.adjustsFontSizeToFitWidth = YES;
    
    //描述
    self.detilLabel = [UILabel new];
    [self.burredImageview addSubview:self.detilLabel];
    
    [self.detilLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.timeLabel.bottom).with.offset(5);
        make.left.offset(20);
        make.right.offset(-20);
        make.bottom.offset(-25);
        
        
    }];
    
    //    self.detilLabel.backgroundColor = [UIColor redColor];
    self.detilLabel.text = self.modelOfDate.desc;
    self.detilLabel.numberOfLines = 0;
    self.detilLabel.adjustsFontSizeToFitWidth = YES;
    self.detilLabel.textColor = [UIColor whiteColor];
    
    
    //图片动画
    [self.playImageview sd_setImageWithURL:[NSURL URLWithString:self.modelOfDate.modelOfCover.detail] ];
    [self.playImageview.layer addAnimation:[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:1.1f] durTimes:6.0f Rep:MAXFLOAT ] forKey:nil];
    
    //图片模式
    self.playImageview.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.burredImageview sd_setImageWithURL:[NSURL URLWithString:self.modelOfDate.modelOfCover.blurred] ];
    
    self.titleLabel.text = self.modelOfDate.title;
    
    
}

//缩放动画
-(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repertTimes
{
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.fromValue = Multiple;
    animation.toValue = orginMultiple;
    animation.autoreverses = YES;
    animation.repeatCount = repertTimes;
    animation.duration = time;//不设置时候的话，有一个默认的缩放时间.
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    return  animation;
    
}

-(void)tapButton{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 6 / 10)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.3;
    [self.playImageview addSubview:view];
    
    UITapGestureRecognizer *singleReg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction)];
    singleReg.numberOfTapsRequired = 1;
    
    
    [self.playImageview addGestureRecognizer:singleReg];
    self.playImageview.userInteractionEnabled = YES;
    
    UIImageView *mediaView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoPlay@3x.png"] ];
    [view addSubview:mediaView];
    [mediaView makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(70, 70));
        
    }];
    
    
}




//手势事件
-(void)singleTapAction{
    
    PlayingVideoViewController *playing = [[PlayingVideoViewController alloc] init];
//    [PlayingVideoViewController shareMySelf];
    playing.modelOfDate = self.modelOfDate;
    
    NSLog(@"%@", playing);
    [self presentViewController:playing animated:YES completion:^{
        
    }];

   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
